<?php
/**
 * 《询价信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Visit.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/CustomerUtil.class.php');

class Enquiry extends CustomerUtil {
	public $tbl = Constant::tbl_enquiry;

	public $send = array(
		'-' => '请选择',
		'1' => '是',
		'2' => '否',
	);

	public $status = array(
		'-' => '请选择',
		'1' => '未发送',
		'2' => '已发送',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'email' => '收件人',
		'emp' => '业务员',
		'date' => '发送时间',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('send_option', $this->send);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'customer/enquiry/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _my() {
		if ($this->_is_admin()) {
			return '';
		}

		if ($this->_audit('123')) {
			return '';
		}

		return " emp = '" . $_SESSION['id'] . "'";
	}

	function email($controller, $request) {
		$id = $request['id'];

		$row = $this->id(Constant::tbl_email, $id);
		$request['to_s'] = $row['email'];

		return $this->findPost($controller, $request);
	}

	function _where($request) {
		$to_s = $request['to_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($to_s)) {
			$where .= " AND `to` = '" . $to_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row['num_'] = $this->_num($row['id']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'customer/enquiry/list');

		return true;
	}

	function _num($id) {
		$read = $this->_read($id);
		if (is_array($read)) {
			return count($read);
		}

		return 0;
	}

	function _read($enquiry) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE enquiry = '%s' ORDER BY id";
		$sql = sprintf($fmt, Constant::tbl_enquiry_read, $enquiry);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {

			$ret[] = $row;
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		$row = $this->id(Constant::tbl_email, $id);

		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('sign_option', $this->_sign($_SESSION['id']));

		$controller->assign('t1', strtotime('2 hour'));

		$controller->display($request, 'customer/enquiry/add');
		return true;
	}

	function _sign($emp) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND status = '2' ORDER BY id";
		$sql = sprintf($fmt, Constant::tbl_email_sign, $emp);
		debug($sql);
		$result = $db->query($sql);
		$ret = array('-'=>'请选择');
		while ($row = $db->fetch_array($result)) {

			$ret[$row['id']] = $row['name'];
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$from = $request['from'];
		$to = $request['to'];

		$subject = addslashes($request['subject']);
		$content = addslashes($request['content']);

		$send = $request['send'];
		if (empty($send) || $send == '-' || $send == '1') {
			$date = date('Y-m-d H:i:s');
			$status = '2';
		} else {
			$date = $request['date'];
			$status = '1';
		}

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(`from`, `to`, subject, content, date, emp, status, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $from, $to, $subject, $content, $date, $_SESSION['id'], $status);
		// debug($sql);
		$db->query($sql);

		$id = $db->insert_id();
		$content = sprintf("%s\n%s", $content, $this->_img($id));
		if ($status == '2') {
			$this->mail_html($from, $to, $subject, $content);
		}

		return true;
	}

	function batch($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->id(Constant::tbl_enquiry_template, $id);
		
		$controller->assign_by_ref('result', $row);
		
		$day = array(
			'-' => '请选择',
			'3' => '3天',
			'7' => '7天',
			'15' => '15天',

			'30' => '1个月',
			'60' => '2个月',
			'90' => '3个月',
			'180' => '6个月',

			'365' => '1年',
		);
		$controller->assign_by_ref('day_option', $day);

		$num = array(
			'-' => '请选择',
			'10' => '10封',
			'20' => '20封',
			'50' => '50封',
			'100' => '100封',
			'200' => '200封',
			'500' => '500封',
		);
		$controller->assign_by_ref('num_option', $num);

		$controller->assign('t1', strtotime('-3 month'));

		$controller->assign('date', strtotime('2 hour'));

		$type = $request['type'];
		if ($type == '1') {
			$controller->display($request, 'customer/enquiry/batch');
		} else {		
			$controller->display($request, 'customer/enquiry/batch2');
		}
		
		return true;
	}

	function batchPost($controller, $request) {
		$this->pretreat($controller);
		
		$template = $request['id'];

		$email = $request['email'];
		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$day = $request['day'];
		$num = $request['num'];

		$from = $_SESSION['emp']['email'];
		$emp = $_SESSION['id'];
					
		$send = $request['send'];
		if (empty($send) || $send == '-' || $send == '1') {
			$date = date('Y-m-d H:i:s');
		} else {
			$date = $request['date'];
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND status = '1'";
		$sql = sprintf($fmt, Constant::tbl_email, $_SESSION['id']);

		if (!empty($email)) {
			$sql .= " AND email LIKE '%" . $email . "%'";
		}

		if (!empty($t1)) {
			$sql .= " AND createtime > '" . $t1 . "'";
		}

		if (!empty($t2)) {
			$sql .= " AND createtime < '" . $t2 . "'";
		}
		debug($sql);

		if (empty($num) || $num == '-') {
			$num = 10;
		} else {
			$num = intval($num);
		}

		$count = 0;
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if ($count >= $num) {
				break;
			}

			if ($this->_is_send($row['email'], $day)) {
				continue;
			}

			$this->_insert($from, $row['email'], $template, $date, $emp);

			$count++;
		}

		$ret['message'] = sprintf("已成功发送%s封邮件！", $count);

		echo json_encode($ret);
		return true;
	}
	
	function batch2Post($controller, $request) {
		$this->pretreat($controller);
		
		$template = $request['id'];

		$from = $_SESSION['emp']['email'];
		$emp = $_SESSION['id'];
			
		$send = $request['send'];
		if (empty($send) || $send == '-' || $send == '1') {
			$date = date('Y-m-d H:i:s');
		} else {
			$date = $request['date'];
		}

		$list = $request['list'];
		
		$bb = explode(',', $list);
		while (list($k, $email) = each($bb)) {
			$email = trim($email);
			$this->_insert($from, $email, $template, $date, $emp);
		}

		$ret['message'] = sprintf("已成功发送%s封邮件！", count($bb));

		echo json_encode($ret);
		return true;
	}

	function _insert($from, $to, $template, $date, $emp) {
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(`from`, `to`, template, date, emp, status, createtime, modifytime)";
		$fmt .=" VALUES('%s', '%s', '%s', '%s', '%s', '1', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $from, $to, $template, $date, $emp);
		// debug($sql);
		$db->query($sql);
	}

	function _is_send($email, $day) {
		if (empty($day) || $day == '-') {
			debug("day is null");
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE `to` = '%s' AND createtime > DATE_ADD(now(), INTERVAL -%s day)";
		$sql = sprintf($fmt, $this->tbl, $email, $day);
		// debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/enquiry/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$from = $request['from'];
		$to = $request['to'];

		$subject = addslashes($request['subject']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET `from` = '%s', `to` = '%s', subject = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $from, $to, $subject, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}



	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'customer/enquiry/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$template = $this->id(Constant::tbl_enquiry_template, $row['template']);
		$row['subject'] = $template['subject'];
		$row['content'] = $template['content'];

		$row = $this->_view_t($row);
		
		return $row;
	}

	function mail($controller, $request) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE status = 1";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$from = $row['from'];
			$to = $row['to'];
			if (strtotime($row['date']) > time()) {
				continue;
			}
			
			$template = $this->id(Constant::tbl_enquiry_template, $row['template']);
			$subject = $template['subject'];
			$content = $template['content'];

			// $to = 'sunxy@tecmold.com';
			// $to = 'sunxy@winhap.net';
			$content = sprintf("%s\n%s", $content, $this->_img($row['id']));
			$this->mail_html($from, $to, $subject, $content);

			$fmt = "UPDATE %s SET status = 2 WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	function _img($id) {
		$fmt = "<a href='http://www.tecmold.com'><img border='0' src='http://ad.tecmold.comcustomer.php?do=Read.ajax_img&id=%s' alt='www.tecmold.com' /></a>";
		$ret = sprintf($fmt, $id);

		return $ret;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function tj_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$to = 'sunxy@tecmold.com';

		$subject = '邮件发送情况';

		$this->result_s = 20;
		$this->groupby_s = 'emp';

		$body = $this->_tj_a($controller, $request);

		$this->mail_html($from, $to, $subject, $body);

		logdate($controller, $request);
		return true;
	}


	function t($controller, $request) {
		$file = "/home/sunxy/a.txt";

		$s = file_get_contents($file);

		echo base64_decode($s);

		logdate($controller, $request);
		return true;
	}
}
?>
